package lk.service.blogspringboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lk.service.blogspringboot.controller.basic.BasicController;
import lk.service.blogspringboot.domain.ArticleLikeDetail;
import lk.service.blogspringboot.service.ArticleLikeDetailService;
import lk.service.blogspringboot.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章点赞详细信息表(ArticleLikeDetail)表控制层
 *
 * @author LK
 * @since 2024-08-22 10:33:40
 */
@RestController
@RequestMapping("articleLikeDetail")
public class ArticleLikeDetailController extends BasicController {
    /**
     * 服务对象
     */
    @Resource
    private ArticleLikeDetailService articleLikeDetailService;

    /**
     * 全查询   带有分页
     *
     * @param articleLikeDetail 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public R queryAll(ArticleLikeDetail articleLikeDetail) {
        // 获取前端pageNum和pageSize
        int pageNum = articleLikeDetail.getPageNum();
        int pageSize = articleLikeDetail.getPageSize();
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        R r = this.articleLikeDetailService.queryAll(articleLikeDetail);
        PageInfo<ArticleLikeDetail> pageInfo = new PageInfo<>((List<ArticleLikeDetail>) r.getData());
        setReturnData(pageInfo.getTotal(), r);
        return r;
    }


    @GetMapping("/noPageALL")
    public R queryAllNoPage(ArticleLikeDetail articleLikeDetail) {
        return this.articleLikeDetailService.queryAll(articleLikeDetail);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return this.articleLikeDetailService.queryById(id);
    }

    /**
     * 查询liker是否关注 articleId 文章
     */
    @GetMapping("/isLike")
    public R queryIsLike(ArticleLikeDetail articleLikeDetail) {
        Integer articleId = articleLikeDetail.getArticleId();
        String liker = articleLikeDetail.getLiker();

        return this.articleLikeDetailService.queryIsLike(articleId,liker);
    }

    /**
     * 新增数据
     *
     * @param articleLikeDetail 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody ArticleLikeDetail articleLikeDetail) {
        return this.articleLikeDetailService.insert(articleLikeDetail);
    }

    /**
     * 编辑数据
     *
     * @param articleLikeDetail 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody ArticleLikeDetail articleLikeDetail) {
        return this.articleLikeDetailService.update(articleLikeDetail);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return this.articleLikeDetailService.deleteById(id);
    }


    /**
     * 通过用户名、文章id删除点赞数据
     */
    @PostMapping("/deleteLikeByUserNameAndArticleId")
    public R deleteByUserNameAndArticleId(@RequestBody ArticleLikeDetail articleLikeDetail) {
        Integer articleId = articleLikeDetail.getArticleId();
        String liker = articleLikeDetail.getLiker();
        return this.articleLikeDetailService.deleteByUserNameAndArticleId(articleId,liker);
    }

    /**
     * 批量删除数据
     *
     * @param idArr 主键数组
     * @return 删除是否成功
     */
    @PostMapping("/deleteBatch")
    public R deleteByIdBatch(@RequestBody List<Integer> idArr) {
        idArr.forEach((id) -> this.articleLikeDetailService.deleteById(id));
        return R.ok();
    }

}
